/**
 * Ceicei.com Inc.
 * Copyright (c) 2004-2012 All Rights Reserved.
 */
package com.ceicei.taobaoke;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ceicei.project.dal.dataobject.CeiceiItemDO;
import com.ceicei.taobaoke.api.shangpin.ItemGetTest;
import com.ceicei.taobaoke.api.shangpin.UmpPromotionGetTest;
import com.ceicei.util.CeiceiConstants;
import com.ceicei.util.TaobaoResultCode;

/**
 *
 * @author 李威-liwei2672@gmail.com
 * @version $Id: TaobaoMain.java, v 0.1 2012-4-25 上午08:45:08 李威 Exp $
 */
public class TaobaoMain {
    private static Log         logger                     = LogFactory.getLog(TaobaoMain.class);

    public static List<String> noproductpricechangeList   = new ArrayList<String>();
    public static List<String> productpricechangeList     = new ArrayList<String>();
    public static List<String> producterrorList           = new ArrayList<String>();

    public static List<String> nopromotionList            = new ArrayList<String>();
    public static List<String> promotionList              = new ArrayList<String>();
    public static List<String> nopromotionpricechangeList = new ArrayList<String>();
    public static List<String> promotionpricechangeList   = new ArrayList<String>();
    public static List<String> promotionerrorList         = new ArrayList<String>();

    public static List<String> errorNumList               = new ArrayList<String>();

    UmpPromotionGetTest        umpPromotionGetTest;
    ItemGetTest                itemGetTest;

    /**
     * 初始化
     *
     */
    public void init() {
        umpPromotionGetTest = new UmpPromotionGetTest();
        umpPromotionGetTest.init();

        itemGetTest = new ItemGetTest();
        itemGetTest.init();
    }

    public void logout() {

        logger.info("=====有促销的数据=====" + promotionList.size());
        for (String str : promotionList) {
            logger.info(str);
        }
        logger.info("=====促销价格未变的数据=====" + nopromotionpricechangeList.size());
        for (String str : nopromotionpricechangeList) {
            logger.info(str);
        }
        logger.info("=====促销价格有变化的数据插入ceicei_items_price_log====="
                    + promotionpricechangeList.size());
        for (String str : promotionpricechangeList) {
            logger.info(str);
        }
        logger.info("=====促销有错误的数据=====" + promotionerrorList.size());
        for (String str : promotionerrorList) {
            logger.info(str);
        }
        logger.info("=====无促销--商品价格的数据=====" + nopromotionList.size());
        for (String str : nopromotionList) {
            logger.info(str);
        }
        logger.info("=====商品价格未变的数据=====" + noproductpricechangeList.size());
        for (String str : noproductpricechangeList) {
            logger.info(str);
        }
        logger.info("=====商品价格有变化的数据插入ceicei_items_price_log=====" + productpricechangeList.size());
        //插入ceicei_items_price_log的数据
        for (String str : productpricechangeList) {
            logger.info(str);
        }
        logger.info("=====商品价格有错误的数据=====" + producterrorList.size() + "--"
                    + promotionerrorList.size());
        for (String str : producterrorList) {
            logger.info(str);
        }
        logger.info("=====错误次数超过了CEICEI的规定=====" + errorNumList.size());
        for (String str : errorNumList) {
            logger.info(str);
        }
        logger.info("");
        logger.info("");
        logger.info("");
        logger.info("=====有促销的数据=====" + promotionList.size());
        logger.info("=====促销价格未变的数据=====" + nopromotionpricechangeList.size());
        logger.info("=====促销价格有变化的数据插入ceicei_items_price_log====="
                    + promotionpricechangeList.size());
        logger.info("=====促销有错误的数据=====" + promotionerrorList.size());

        logger.info("=====无促销--商品价格的数据=====" + nopromotionList.size());
        logger.info("=====商品价格未变的数据=====" + noproductpricechangeList.size());
        logger.info("=====商品价格有变化的数据插入ceicei_items_price_log=====" + productpricechangeList.size());
        logger.info("=====商品价格有错误的数据=====" + producterrorList.size());

        logger.info("=====错误次数超过了CEICEI的规定=====" + errorNumList.size());
    }

    /**
     *
     * @param args
     */
    public static void go() {
        TaobaoMain go = new TaobaoMain();

        go.init();
        go.run();
        go.logout();
    }

    /**
     * 开始处理收藏的商品
     *
     */
    public void run() {
        //处理taobao的商品
        getBySource("taobao");

        //处理tianmao的商品
        getBySource("tianmao");
    }

    /**
     * 处理相同来源的一批商品
     *
     * @param source
     */
    private void getBySource(String source) {
        logger.info("=====" + source + "=====");

        //获取收藏的商品
        List<CeiceiItemDO> ceiceiItemDOList = umpPromotionGetTest.getCeiceiItemDAO().getBySource(
            source);

        if (ceiceiItemDOList != null && ceiceiItemDOList.size() > 0) {

            for (CeiceiItemDO ceiceiItemDO : ceiceiItemDOList) {
                processItem(ceiceiItemDO,false);
            }
        }
    }

    /**
     * 
     * 处理单个商品
     *
     * @param ceiceiItemDO
     */
    public void processItem(CeiceiItemDO ceiceiItemDO,boolean single) {
        //        if (ceiceiItemDO.getItemid() != 42)
        //            return;

        /**
         * 1)   
         * 首先获取促销价格，如果有促销，则将促销价保持在ceicei_items_price_log，并标记该价格为促销价格，
         * 此时不需要获取商品价格
         * */

        //对失败商品循环处理CeiceiConstants.ERROR_NUM次
        int errorCount = 0;

        //调用taobao的结果
        TaobaoResultCode result = null;

        //循环超过规定次数，退出
        while (errorCount < CeiceiConstants.ERROR_NUM) {
            //            logger.info(ceiceiItemDO.getItemid() + "=====\t第" + (++errorCount)
            //                        + "次促销价格处理\t=====");

            result = umpPromotionGetTest.callTaobaoAPI(ceiceiItemDO,single);
            //调用成功、没有促销、商品不存在、商品被删除，退出
            if (result == TaobaoResultCode.SUCCESS || result == TaobaoResultCode.NO_PROMOTION
                || result == TaobaoResultCode.NOT_EXIST || result == TaobaoResultCode.DELETED) {
                break;
            }
        }
        if (errorCount >= CeiceiConstants.ERROR_NUM) {
            //            logger.info(ceiceiItemDO.getItemid() + "\t促销价格处理次数超过规定次数：" + CeiceiConstants.ERROR_NUM);
            errorNumList.add(ceiceiItemDO.getItemid() + "\t促销价格处理次数超过规定次数："
                             + CeiceiConstants.ERROR_NUM);
        }

        //调用成功、商品不存在，处理下一个商品
        if (result == TaobaoResultCode.SUCCESS || result == TaobaoResultCode.NOT_EXIST
            || result == TaobaoResultCode.DELETED) {
            return;
        }

        /**
         * 2)   如果获取不到促销价格（没有促销，或者循环超过规定次数），
         * 则获取商品价格，并将商品价格保存到ceicei_items_price_log
         * */

        //没有促销
        if (result == TaobaoResultCode.NO_PROMOTION) {
            errorCount = 0;

            while (errorCount < CeiceiConstants.ERROR_NUM) {
                //                logger.info(ceiceiItemDO.getItemid() + "=====\t第" + (++errorCount)
                //                            + "次商品价格处理\t=====");
                result = itemGetTest.callTaobaoAPI(ceiceiItemDO,single);
                //调用成功，，退出
                if (result == TaobaoResultCode.SUCCESS || result == TaobaoResultCode.NO_PROMOTION
                    || result == TaobaoResultCode.NOT_EXIST || result == TaobaoResultCode.DELETED) {
                    break;
                }
            }
            if (errorCount >= CeiceiConstants.ERROR_NUM) {
                logger.info(ceiceiItemDO.getItemid() + "\t商品价格处理次数超过规定次数："
                            + CeiceiConstants.ERROR_NUM);
            }
        }
    }
}
